﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using CommonUtility;
using DAL;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace BLL
{
    public class bllAction
    {
        /// <summary>
        /// 根据条件获取数据
        /// </summary>
        /// <param name="_condition"></param>
        /// <param name="_oUser"></param>
        /// <returns></returns>
        public string GetActInf(string _condition, UserInfo _operator)
        {
            string result = "";
            try
            {
                IList<ActionInfo> list = new List<ActionInfo>();
                list = DAL.Action.GetList(_condition);
                string json = new JavaScriptSerializer().Serialize(list);
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllAction.cs/GetActInf(" + _condition + ") " + _operator.Name, ex);
            }
            return result;
            
        }

        /// <summary>
        /// 获取所有Action
        /// </summary>
        /// <param name="_funID">记录Log用</param>
        /// <param name="_actID">ActID</param>
        /// <param name="_actDesc">ActDesc</param>
        /// <param name="_oUser">权限验证</param>
        /// <returns>string</returns>
        public string GetActInf(string _actID, string _actName, UserInfo _operator)
        {
            string strSQL = "1=1";
            SqlParameter[] paras = null;
            IList<SqlParameter> paraList = new List<SqlParameter>();
            string result = string.Empty;

            if (!string.IsNullOrEmpty(_actID))
            {
                strSQL += " and ActID like '%'+@ActID+'%'";
                paraList.Add(new SqlParameter("@ActID", SqlDbType.VarChar, 3) { Value = _actID });
            }
            if (!string.IsNullOrEmpty(_actName))
            {
                strSQL += " and ActName like '%'+@ActName+'%'";
                paraList.Add(new SqlParameter("@ActName", SqlDbType.NVarChar, 50) { Value = _actName });
            }
            if (paraList.Count > 0)
            {
                paras = paraList.ToArray();
            }

            try
            {
                IList<ActionInfo> list = new List<ActionInfo>();
                list = DAL.Action.GetList(strSQL, paras);
                string json = new JavaScriptSerializer().Serialize(list.OrderByDescending(d => d.RID).ToList());
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Common.Application_Error_Server("BLL/bllAction.cs/LoadList(" + strSQL + ") " + _operator.Name, ex);
            }
            return result;
        }

        /// <summary>
        /// 获取所有Action
        /// </summary>
        /// <param name="_rID"></param>
        /// <param name="_actID"></param>
        /// <param name="_actDesc"></param>
        /// <param name="_oUser"></param>
        /// <returns>List</returns>
        public IList<ActionInfo> CheckIfExist(string _rID, string _actID, string _actName, UserInfo _operator)
        {
            string strSQL = string.Empty;
            string result = string.Empty;
            SqlParameter[] paras = new SqlParameter[2];
            //查找ActName相同但ID不同的Act
            paras[0] = new SqlParameter("@ActName", SqlDbType.NVarChar, 100) { Value = _actName };
            if (!string.IsNullOrEmpty(_rID))
            {
                strSQL = " ActName = @ActName AND RID <> @RID";
                paras[1] = new SqlParameter("@RID", SqlDbType.Int, 4) { Value = _rID };
            }
            else
            {
                strSQL = " ActName = @ActName OR ActID = @ActID ";
                paras[1] = new SqlParameter("@ActID", SqlDbType.NVarChar, 3) { Value = _actID };
            }
            //查询结果list
            IList<ActionInfo> list = new List<ActionInfo>();
            try
            {
                list = DAL.Action.GetList(strSQL, paras);
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Common.Application_Error_Server("BLL/bllAction.cs/GetAllActionInfo(" + strSQL + ") " + _operator.Name, ex);
            }
            return list;
        }

        /// <summary>
        /// 新增或保存
        /// </summary>
        /// <param name="_funID"></param>
        /// <param name="_model"></param>
        /// <param name="_oUser"></param>
        /// <returns>bool</returns>
        public bool Add(string _funID, ActionInfo _model, UserInfo _operator)
        {
            bool res = true;
            //string _desc = "Add/新增：" + _model.ActID;//添加新记录操作
            string _desc = "Add";
            try
            {
                if (!string.IsNullOrEmpty(_model.RID.ToString()) && !_model.RID.ToString().Equals("0"))
                {
                    //_desc = "Update/保存：" + _model.ActID;//保存操作
                    _desc = "Update";//保存操作
                }
                DAL.Action.Add(_model);

                //添加操作记录
                bllLog.AddLog(_funID, _desc, _model.ActID, _operator.UserID);
            }
            catch (Exception ex)
            {
                Common.Application_Error_Server("BLL/bllAction.cs/DataSave(" + _model.RID + ") " + _operator.Name, ex);
                res = false;
            }

            return res;
        }

        /// <summary>
        /// 设置是否有效
        /// </summary>
        /// <param name="_funID"></param>
        /// <param name="_rids"></param>
        /// <param name="_status">A OR I</param>
        /// <param name="_oUser"></param>
        /// <returns></returns>
        public string Process(string _funID, string _rids, string _status, UserInfo _operator)
        {
            string result = string.Empty;
            string _type = "Active";//Active
            string actid = string.Empty;
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection myConn = myTran.Connection;
            try
            {
                IList<ActionInfo> list = DAL.Action.GetList(" Rid in (" + _rids + ")");
                actid = string.Join(",", list.Select(d => d.ActID).ToArray());
                foreach (var Actinfo in list)
                {
                    Actinfo.Flag = _status;
                    Actinfo.Whom = _operator.UserID;
                    DAL.Action.Edit(Actinfo, myTran);
                }
                if (_status.Equals("I"))
                {
                    _type = "InAction";//InAction
                }
                //添加操作记录
                bllLog.AddLog(_funID, _type, _rids, _operator.UserID, myTran);

                myTran.Commit();
            }
            catch (Exception ex)
            {
                result = ex.Message;
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllAction.cs/Process(" + _rids + ") " + _operator.Name, ex);
            }
            finally
            {
                myConn.Close();
                myTran.Dispose();
                myConn.Dispose();
            }

            return result;
        }

        /// <summary>
        /// 获取描述
        /// </summary>
        /// <param name="_ActID"></param>
        /// <returns></returns>
        public static string GetActDesc(string _ActID)
        {
            string res = string.Empty;
            try
            {
                IList<ActionInfo> list = DAL.Action.GetList(" ActID = '" + _ActID + "'");
                if (list.Count > 0)
                {
                    res = string.Join(",", list.Select(d => d.ActName).ToArray());
                }
            }
            catch (Exception ex)
            {
                Common.Application_Error_Server("bllAction.cs/GetActDesc(" + _ActID + ")", ex);
            }
            return res;
        }
    }
}
